<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Members_pi
{
	function js($txt='')
	{
		return "<script type'text/javascript'>$txt</script>";
	}

	function flash1($txt='',$type='')
	{
		return "<div class='flash $type'>$txt</div>";
	}

	function flash($txt='',$type='')
	{
		if ($type!='') $type="$type";
		return "<div class='fmessage $type'>$txt</div>".js("setTimeout('fadeMessage()',3000); $('.nmessage').css('z-index',999);");
	}

	function getURL()
		{
		$loc = $_SERVER['PHP_SELF'];
		$offset = strpos($loc,"index.php")+10;
		return substr($loc,$offset,strlen($loc)-$offset);
		}

	function fixQts($strOrig) {
		if ($strOrig=="") return false;
		return trim(str_replace("'", "`", str_replace("�","&quot;", str_replace("\n","\\n",$strOrig))));
	}

	function zonedate($layout, $countryzone = 8, $daylightsaving = false)
	{
	if ($daylightsaving){
	$daylight_saving = date('I');
	if ($daylight_saving){$zone=3600*($countryzone+1);}
	}
	else {
	   if ($countryzone>>0){$zone=3600*$countryzone;}
		   else {$zone=0;}
	}
	$date=gmdate($layout, time() + $zone);
	return $date;
	}

	function getNow()
	{
		$n = zonedate('Y-m-d H:i:s',8,false);
		return $n;
	}

	function getmonth($month)
	{
		switch ($month) {
		case "Jan":
			$m = 1;
			break;
		case "Feb":
			$m = 2;
			break;
		case "Mar":
			$m = 3;
			break;
		case "Apr":
			$m = 4;
			break;
		case "May":
			$m = 5;
			break;
		case "Jun":
			$m = 6;
			break;
		case "Jul":
			$m = 7;
			break;
		case "Aug":
			$m = 8;
			break;
		case "Sep":
			$m = 9;
			break;
		case "Oct":
			$m = 10;
			break;
		case "Nov":
			$m = 11;
			break;
		case "Dec":
			$m = 12;
			break;
		}	
		return $m;
	}

	function convertDate($date,$time="0:0:0") {
		
		$date_arr = explode(' ', $date);
		$dt = explode("/",$date_arr[0]);
		if (isset($date_arr[1])) $tm = explode(":",$date_arr[1]); else $tm = explode(":",$time);
		
		if ($time!="0:0:0" && $time!="00:00:00") $st = 'H:i:s'; else $st = "";
		return date("Y-m-d $st",mktime($tm[0],$tm[1],$tm[2],$dt[0],$dt[1],$dt[2]));
	}

	function convertViewDate($date,$time="0:0:0") {
		
		$date_arr = explode(' ', $date);
		$dt[0] = getMonth($date_arr[0]);
		$dt[1] = str_replace(",","",$date_arr[1]); //substr($date_arr[1],0,2)
		$dt[2] = $date_arr[2];
		if (isset($date_arr[3])) $tm = explode(":",$date_arr[1]); else $tm = explode(":",$time);
		

		if ($time!="0:0:0" && $time!="00:00:00") $st = 'H:i:s'; else $st = "";
		return date("Y-m-d",mktime(0,0,0,$dt[0],$dt[1],$dt[2]));
	}

	function convertDateDisplay($date,$showtime=true,$type="") {
		if (!$date) return $date;
		if ($date=='0000-00-00 00:00:00') return '';
		$dt = explode(" ",$date);
		$tfr = explode("-",$dt[0]);
		if ($showtime==true) $tm = explode(":",$dt[1]);
		
		
		if ($showtime==true && $type == "") return date('m/d/Y H:i:s',mktime($tm[0],$tm[1],$tm[2],$tfr[1],$tfr[2],$tfr[0]));
		elseif ($showtime==true && $type == "1") return date('F d, Y H:i:s',mktime($tm[0],$tm[1],$tm[2],$tfr[1],$tfr[2],$tfr[0]));
		elseif ($showtime==false && $type == "1") return date('F d, Y',mktime(0,0,0,$tfr[1],$tfr[2],$tfr[0]));
		else return date('m/d/Y',mktime(0,0,0,$tfr[1],$tfr[2],$tfr[0]));
	}

	function firstDay() {
	return date("m/d/Y", mktime(0, 0, 0, date("m")-1, date("d")+1, date("Y")));
	}

	function lastDay() {
	return date("m/d/Y", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
	}

	#
	function count_days( $a, $b )
	#
	{
	#
	// First we need to break these dates into their constituent parts:
	#
	$gd_a = explode("/", $a );
	#
	$gd_b = explode("/", $b );
	#
	 
	#
	// Now recreate these timestamps, based upon noon on each day
	#
	// The specific time doesn't matter but it must be the same each day
	#
	$a_new = mktime( 12, 0, 0, $gd_a['0'], $gd_a['1'], $gd_a['2'] );
	#
	$b_new = mktime( 12, 0, 0, $gd_b['0'], $gd_b['1'], $gd_b['2'] );
	#
	 
	#
	// Subtract these two numbers and divide by the number of seconds in a
	#
	// day. Round the result since crossing over a daylight savings time
	#
	// barrier will cause this time to be off by an hour or two.
	#
	return round( abs( $a_new - $b_new ) / 86400 );
	#
	}



	function getWords($string,$n) {
		
		$words = preg_split("/[\s,]+/", $string);
		$firstN = array_slice($words, 0, $n);
		$firstNAsAString = implode(" ", $firstN);
		
		if (strlen($string)>strlen($firstNAsAString)) $firstNAsAString .= "...";
		
		return $firstNAsAString;
	}


	function countRec($fname,$tname) {
		$sql = "SELECT count($fname) FROM $tname ";
		//echo $sql;
		$result = mysql_query($sql);
		while ($row = mysql_fetch_array($result)) {
			return $row[0];
		}	
	}	

	function runSQL($rsql) {
		$result = mysql_query($rsql);
		return $result;
	}

	function getRec($fname,$tname) {
		$sql = "SELECT $fname FROM $tname LIMIT 1 ";
		//echo $sql;
		$result = mysql_query($sql);
		while ($row = mysql_fetch_array($result)) {
		return $row[0];
		}
	}

	function mkdir_r($dirName, $rights){
		$dirs = explode('/', $dirName);
		$dir='';
		foreach ($dirs as $part) {
			$dir.=$part.'/';
			if (!is_dir($dir) && strlen($dir)>0) mkdir($dir, $rights);
		}
	}

	function generatePassword ($length) {

		$password = "";
		
		$possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
		
		$i = 0; 
		
		while ($i < $length) { 
		
		$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
			if (!strstr($password, $char)) { 
			  $password .= $char;
			  $i++;
			}
		
		}
		
		return $password;
	}

	function convert_number($number) 
	{ 
		if (($number < 0) || ($number > 999999999)) 
		{ 
		throw new Exception("Number is out of range");
		} 

		$Gn = floor($number / 1000000);  /* Millions (giga) */ 
		$number -= $Gn * 1000000; 
		$kn = floor($number / 1000);     /* Thousands (kilo) */ 
		$number -= $kn * 1000; 
		$Hn = floor($number / 100);      /* Hundreds (hecto) */ 
		$number -= $Hn * 100; 
		$Dn = floor($number / 10);       /* Tens (deca) */ 
		$n = $number % 10;               /* Ones */ 

		$res = ""; 

		if ($Gn) 
		{ 
			$res .= convert_number($Gn) . " Million"; 
		} 

		if ($kn) 
		{ 
			$res .= (empty($res) ? "" : " ") . 
				convert_number($kn) . " Thousand"; 
		} 

		if ($Hn) 
		{ 
			$res .= (empty($res) ? "" : " ") . 
				convert_number($Hn) . " Hundred"; 
		} 

		$ones = array("", "One", "Two", "Three", "Four", "Five", "Six", 
			"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", 
			"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eightteen", 
			"Nineteen"); 
		$tens = array("", "", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", 
			"Seventy", "Eigthy", "Ninety"); 

		if ($Dn || $n) 
		{ 
			if (!empty($res)) 
			{ 
				$res .= " and "; 
			} 

			if ($Dn < 2) 
			{ 
				$res .= $ones[$Dn * 10 + $n]; 
			} 
			else 
			{ 
				$res .= $tens[$Dn]; 

				if ($n) 
				{ 
					$res .= "-" . $ones[$n]; 
				} 
			} 
		} 

		if (empty($res)) 
		{ 
			$res = "zero"; 
		} 

		return $res; 
	} 

	/**
	 * see if the java extension was loaded.
	 */
	function checkJavaExtension()
	{
		if(!extension_loaded('java'))
		{
			$sapi_type = php_sapi_name();
			$port = (isset($_SERVER['SERVER_PORT']) && (($_SERVER['SERVER_PORT'])>1024)) ? $_SERVER['SERVER_PORT'] : '8080';
			if ($sapi_type == "cgi" || $sapi_type == "cgi-fcgi" || $sapi_type == "cli") 
			{
				if(!(PHP_SHLIB_SUFFIX=="so" && @dl('java.so'))&&!(PHP_SHLIB_SUFFIX=="dll" && @dl('php_java.dll'))&&!(@include_once("java/Java.inc"))&&!(require_once("http://127.0.0.1:$port/java/Java.inc"))) 
				{
					return "java extension not installed.";
				}
			} 
			else
			{
				if(!(@include_once("java/Java.inc")))
				{
					require_once("http://127.0.0.1:$port/java/Java.inc");
				}
			}
		}
		if(!function_exists("java_get_server_name")) 
		{
			return "The loaded java extension is not the PHP/Java Bridge";
		}

		return true;
	}

	/** 
	 * convert a php value to a java one... 
	 * @param string $value 
	 * @param string $className 
	 * @returns boolean success 
	 */  
	function convertValue($value, $className)  
	{  
		// if we are a string, just use the normal conversion  
		// methods from the java extension...  
		try   
		{  
			if ($className == 'java.lang.String')  
			{  
				$temp = new Java('java.lang.String', $value);  
				return $temp;  
			}  
			else if ($className == 'java.lang.Boolean' ||  
				$className == 'java.lang.Integer' ||  
				$className == 'java.lang.Long' ||  
				$className == 'java.lang.Short' ||  
				$className == 'java.lang.Double' ||  
				$className == 'java.math.BigDecimal')  
			{  
				$temp = new Java($className, $value);  
				return $temp;  
			}  
			else if ($className == 'java.sql.Timestamp' ||  
				$className == 'java.sql.Time')  
			{  
				$temp = new Java($className);  
				$javaObject = $temp->valueOf($value);  
				return $javaObject;  
			}  
		}  
		catch (Exception $err)  
		{  
			echo (  'unable to convert value, ' . $value .  
					' could not be converted to ' . $className);  
			return false;  
		}
	  
		echo (  'unable to convert value, class name '.$className.  
				' not recognised');  
		return false;  
	}
}

//checkJavaExtension();
?>